
import Vue from 'vue'
import {
    RECEIVE_ADDRESS,
    RECEIVE_CATEGORYS,
    RECEIVE_SHOPS,
    RECEIVE_USER_INFO,
    RESET_USER_INFO,
    RECEIVE_GOODS,
    RECEIVE_RATINGS,
    RECEIVE_INFO,
    INCREMENT_FOOD_COUNT,
    DECREMENT_FOOD_COUNT,
    CLEAR_CART,
    RECEIVE_SEARCH_SHOPS
} from './mutation-types'
export default {
    // 存储地址信息
    [RECEIVE_ADDRESS](state, { address }) {
        state.address = address
    },
    // 存储分类
    [RECEIVE_CATEGORYS](state, { categoryx }) {
        state.categorys = categoryx
    },
    // 存储商家信息
    [RECEIVE_SHOPS](state, { shops }) {
        state.shops = shops
    },
    // 存储个人信息
    [RECEIVE_USER_INFO](state, { userInfo }) {
        state.userInfo = userInfo
    },
    // 重置个人信息
    [RESET_USER_INFO](state) {
        state.userInfo = {}
    },
    // 商家商品goods数组获取
    [RECEIVE_GOODS](state, { goods }) {
        state.goods = goods
    },
    // 商家商品评价数组获取
    [RECEIVE_RATINGS](state, { ratings }) {
        state.ratings = ratings
    },
    //商家商品info个人信息获取
    [RECEIVE_INFO](state, { info }) {
        state.info = info
    },
    // input数量增加
    [INCREMENT_FOOD_COUNT](state,{food}){
        if(food.count){
            food.count++ //不能实现双向数据绑定
        }else {
            // food.count=1
            Vue.set(food,'count',1)
            // 每次加一件商品的时候也需要吧这件商品加入购物车
            state.cartFoods.push(food)
        }
    },
    // input数量减少
    [DECREMENT_FOOD_COUNT](state,{food}){
        if(food.count){
            food.count--;
            // 减少商品的时候 如果这个商品的数量已经减到0了 那么需要把这个商品从购物车里面移除
            if(food.count==0){
                state.cartFoods.splice(state.cartFoods.indexOf(food),1)
            }
        }
    },
    // 清空购物车
    [CLEAR_CART](state){
        state.cartFoods.forEach(food=>food.count=0) //把后面追加的count也变为0
        state.cartFoods=[] //清空购物车
    },
    [RECEIVE_SEARCH_SHOPS](state,{searchShops}){
        state.searchShops=searchShops
    }
}